﻿package net.pezzato.genetree.graphics {
	import net.pezzato.localization.Translator;
	import fl.controls.TextArea;
	import flash.display.Sprite;
	import flash.events.Event;
	/**
	 * ...
	 * @author Alessandro Pezzato
	 */
	public class InfoWidget extends Sprite {
		private var ta_:TextArea;
		public function InfoWidget(xml:XML, w:Number, h:Number) {
			drawTextArea(w, h);
			writeText(xml);
			hide();
		}
		public function show():void {
			this.visible = true;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		public function hide():void {
			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			this.visible = false;
			this.alpha = 0;
		}
		private function writeText(info:XML):void {
			if (info != null) {
				var list:XMLList = info.children();
				for each (var e:XML in list) {
					var name:String = e.name();
					var content:String = e.toString();
					if (content.length > 0) {
						ta_.htmlText += ("<b>" + Translator.translate(name) + "</b>: " + content + "\n");
					}
				}
			} else {
				ta_.appendText(Translator.translate("infoNotAvaliable"));
			}
		}
		private function drawTextArea(w, h):void {
			ta_ = new TextArea();
			ta_.width = w;
			ta_.height = h;
			ta_.horizontalScrollPosition = 0;
			addChild(ta_);
			ta_.wordWrap = true;
			mouseChildren = false;
			ta_.mouseEnabled = false;
		}
		private function onEnterFrame(event:Event):void {
			if (alpha < 1) {
				alpha += 0.05;
			}
		}
	}
}